import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:package_info/package_info.dart';
import 'package:proj_shop/tab_navigation/pages/home1_page.dart';
import 'package:proj_shop/tab_navigation/pages/me_page.dart';
import 'package:proj_shop/tab_navigation/pages/picture_page.dart';
import 'package:proj_shop/tab_navigation/pages/search_page.dart';

class TabNavigation extends StatefulWidget {
  @override
  _TabNavigationState createState() => _TabNavigationState();
}

class _TabNavigationState extends State<TabNavigation> {
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;
  DateTime _lastPressDateTime; //上次点击时间
  PageController _pageController = PageController(initialPage: 0);
  int _currentIndex = 0;

  @override
  void initState() {
    getPackageInfo();
    super.initState();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WillPopScope(
        child: PageView(
          physics: NeverScrollableScrollPhysics(),
          controller: _pageController,
          children: [
            Home1Page(),
            SearchPage(),
            PicturePage(),
            MePage(),
          ],
        ),
        onWillPop: exitApp,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          _bottomNavigationBarItem(
              Icons.home, '首页', 0, _currentIndex, _defaultColor, _activeColor),
          _bottomNavigationBarItem(Icons.search, '搜索', 1, _currentIndex,
              _defaultColor, _activeColor),
          _bottomNavigationBarItem(Icons.camera_alt, '旅拍', 2, _currentIndex,
              _defaultColor, _activeColor),
          _bottomNavigationBarItem(Icons.account_circle, '我的', 3, _currentIndex,
              _defaultColor, _activeColor),
        ],
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        selectedFontSize: 12,
        unselectedFontSize: 12,
        onTap: (index) {
          _pageController.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }

  Future<bool> exitApp() {
    if (_lastPressDateTime == null ||
        DateTime.now().difference(_lastPressDateTime) > Duration(seconds: 2)) {
      Fluttertoast.showToast(
          msg: "再按一次退出应用",
          backgroundColor: Colors.grey,
          toastLength: Toast.LENGTH_SHORT,
          fontSize: 14);
      //两次点击间隔超过2秒则重新计时
      _lastPressDateTime = DateTime.now();
      return Future.value(false);
    }
    return Future.value(true);
    /*showDialog(
      context: context,
      builder: (_) {
        return AlertDialog(
          content: Text('是否退出APP'),
          actions: [
            FlatButton(onPressed: () {}, child: Text('取消')),
            FlatButton(
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
                child: Text('确定')),
          ],
        );
      },
    );*/
  }
}

/// 获取包的信息
void getPackageInfo() async {
  PackageInfo packageInfo = await PackageInfo.fromPlatform();
  String appName = packageInfo.appName;
  String packageName = packageInfo.packageName;
  String version = packageInfo.version;
  String buildNumber = packageInfo.buildNumber;
  print(
      'appName:$appName,packageName:$packageName,version:$version,buildNumber:$buildNumber}');
// appName:null,packageName:com.fun.more,version:1.0.0,buildNumber:1}
}

BottomNavigationBarItem _bottomNavigationBarItem(
    IconData iconData,
    String title,
    int index,
    int currentIndex,
    Color defaultColor,
    Color activeColor) {
  return BottomNavigationBarItem(
    icon: Icon(
      iconData,
      color: defaultColor,
    ),
    activeIcon: Icon(
      iconData,
      color: activeColor,
    ),
    title: Text(
      title,
      style: TextStyle(
        color: currentIndex != index ? defaultColor : activeColor,
      ),
    ),
  );
}
